$(function () {
    //传回用户名
    var da = {
        "user": getCookie("userName")
    };
    //添加下拉选择问卷
    var selectContent = ''; //下拉选择内容
    jQuery.ajax({
        type: "POST",
        url: httpRequestUrl + "/queryAllQuestionnaireByCreated",
        data: JSON.stringify(da),
        dataType: 'json',
        contentType: "application/json",
        success: function (result) {
            for (var i = 0; i < result.data.length; i++) {
                selectContent += '<option value="' + result.data[i].id + '">' +
                    result.data[i].questionName + '</option>'
            }
            $("#ddlActivitynew").html(selectContent)
            $("#ddlActivitynew option[value='" + getCookie("questionId") + "']").attr(
                "selected", "selected");
            initPreviewPanel();
        }
    });
});

//    切换问卷
$("#ddlActivitynew").change(function () {
    var activity = $(this).val();
    var nameQuestion = $(this)[0].selectedOptions[0].innerHTML;
    if (activity) {
        deleteCookie("questionId");
        setCookie("questionId", activity)
        deleteCookie("nameOfQuestionnaire");
        setCookie("nameOfQuestionnaire", nameQuestion)
        $("#questionNameDetail").html("<"+
            getCookie("nameOfQuestionnaire") + ">答题情况明细");
        initPreviewPanel();
    }
})

//设计问卷
function designQuestionnaire() {
    var ifDesignQuestionnaire = getCookie("ifDesignQuestionnaire");
    if (ifDesignQuestionnaire === "false") {
        layer.msg("问卷处于运行状态或问卷已发布，不可设计问卷", {icon: 2})
    } else {
        $.cookie("QuestionId", getCookie("questionId"));
        window.open('designQuestionnaire.html')
    }
}

//发送问卷
function ifSendQuestionnaire() {
    var ifSendQuestionnaire = getCookie("ifSendQuestionnaire");
    if (ifSendQuestionnaire === "false") {
        layer.msg("问卷处于未发送或暂停的状态，不可发送问卷", {icon: 2})
    } else {
        $.cookie("QuestionId", getCookie("questionId"));
        window.location.href = 'sendQuestionnaire.html';
    }
}

//预览问卷
$('#ctl02_hrefView').click(function () {
    window.open('previewQuestionnaire.html?i=' + getCookie("questionId") + '&e0Rp=preview');
});

//返回主页
function backhome() {
    window.location.href = 'myQuestionnaires.html'
}

//插入预览答卷页面
function initPreviewPanel() {
    //清空panel_previewQuestionnaire的内容
    $("#panel_dataScreen").empty();
    //向面板中插入预览答卷页面
    var iframe = document.createElement("iframe");
    iframe.src = "echartsTest.html";
    iframe.style.width = "100%";
    iframe.style.height = "100%";
    iframe.style.border = "0";
    iframe.style.margin = "0";
    //隐藏iframe中的select_answer_change
    iframe.onload = function () {
        iframe.contentWindow.document.getElementById("select_answer_change").style.display = "none";
    }
    //向panel_previewQuestionnaire中插入iframe
    document.getElementById("panel_dataScreen").appendChild(iframe);
}